/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MenuPrincipal;

import apunte.Apunte;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author ubuntu
 */
public class FormularioCantidad extends javax.swing.JFrame {

    /**
     * Creates new form FormularioCantidad
     */
    private Apunte ap;
    private LinkedList<Apunte> llaVenta, lla;
    private DefaultListModel lista;
    private MenuPrincipal mp;

    public FormularioCantidad() {
        initComponents();
        
        Lapunte.setText(ap.getNombreApunte());
    }

    FormularioCantidad(Apunte get, LinkedList<Apunte> llaVenta1, DefaultListModel modelLista, MenuPrincipal mp1, LinkedList<Apunte> lla1) {
        super("Cantidad");
        ap = get;
        mp = mp1;
        llaVenta = new LinkedList();
        lla = new LinkedList();
        lla = lla1;
        llaVenta = llaVenta1;
        lista = modelLista;
        

        initComponents();

        Lapunte.setText(ap.getNombreApunte());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        Baceptar = new javax.swing.JButton();
        Bcancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        Lapunte = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        LCantidad = new javax.swing.JTextField();

        jLabel3.setText("jLabel3");

        Baceptar.setText("Aceptar");
        Baceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BaceptarActionPerformed(evt);
            }
        });

        Bcancelar.setText("Cancelar");
        Bcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BcancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Apunte");

        Lapunte.setText("  ");

        jLabel4.setText("Cantidad");

        LCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LCantidadActionPerformed(evt);
            }
        });
        LCantidad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                LCantidadFocusGained(evt);
            }
        });
        LCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                LCantidadKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Baceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Bcancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(LCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(37, 37, 37)
                        .addComponent(Lapunte)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(Lapunte))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(LCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Baceptar)
                    .addComponent(Bcancelar))
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void LCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LCantidadActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_LCantidadActionPerformed

    private void LCantidadFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_LCantidadFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_LCantidadFocusGained

    private void BcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BcancelarActionPerformed
        setVisible(false);
    }//GEN-LAST:event_BcancelarActionPerformed

    private void BaceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BaceptarActionPerformed

        if (Integer.parseInt(LCantidad.getText()) <= ap.getCantidad()) {

            btnAceptar();
            
         



        } else {

            JOptionPane.showMessageDialog(null, "No hay suficientes apuntes");

        }


    }//GEN-LAST:event_BaceptarActionPerformed

    private void LCantidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_LCantidadKeyPressed

        
        if (evt.getKeyCode() == 10){
            btnAceptar();
        }
        
        if (evt.getKeyCode() == 27){
            setVisible(false);
        }
        
    }//GEN-LAST:event_LCantidadKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormularioCantidad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormularioCantidad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormularioCantidad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormularioCantidad.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormularioCantidad().setVisible(true);
                
                
                
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Baceptar;
    private javax.swing.JButton Bcancelar;
    private javax.swing.JTextField LCantidad;
    private javax.swing.JLabel Lapunte;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables

    private void actualizoLista() {


        lista.clear();



        for (int i = 0; i < llaVenta.size(); i++) {

            lista.addElement(llaVenta.get(i).getNombreApunte());


        }


    }

    private void descuentoDeTabla() {
    }

    private void restoEnTabla(LinkedList<Apunte> lla, Apunte ap) {



        for (int i = 0; i < lla.size(); i++) {
            if (lla.get(i).getIdapunte() == ap.getIdapunte()) {

                lla.set(i, ap);
                break;
                
            }


        }
        int cantidadRow = mp.getModelo().getRowCount();
        for (int i = 0; i < cantidadRow; i++) {

            mp.getModelo().borraApunte(0);


        }

        for (int i = 0; i < lla.size(); i++) {

            mp.getModelo().anhadeApunte(lla.get(i));

        }



    }

    private void btnAceptar() {
        
        if (ap.getCantidad() >= Integer.parseInt(  LCantidad.getText())){
           ap.setCantidadVenta(Integer.parseInt(LCantidad.getText()));
            llaVenta.add(ap);
            ap.setCantidad(ap.getCantidad() - Integer.parseInt(LCantidad.getText()));
            restoEnTabla(lla, ap);
            int tot = Integer.parseInt(mp.getPrecioTotal().getText());
            mp.getPrecioTotal().setText(String.valueOf(tot + (Integer.parseInt(ap.getPrecio()) * ap.getCantidadVenta())));

            actualizoLista();
            descuentoDeTabla();

            
        }else{
        
            JOptionPane.showMessageDialog(null, "No hay suficientes apuntes.");
            
        }
        
        mp.getTabla().requestFocus();
        setVisible(false);
    }
}
